import Vue from 'vue'
import Vuex from 'vuex'
import store from '../store'
let vue = Vue.use(Vuex)


function isNotEmpty(row, column) {
  return row.hasOwnProperty(column.property)
    && row[column.property] != null
    && row[column.property] != undefined
}


export default class accountUtils {

   getaccountStates(){
    return [
      { id: 1, name: '正常' }
    ]
  } 

  constructor(options) {
  }

  getAccountColumns() {
    return [
      { prop: 'user', label: '账号', sortable: true, width: '120', formatter: this.stringFormater },
      { prop: 'name', label: '姓名', sortable: true, width: '120', formatter: this.stringFormater },
      { prop: 'state', label: '账号状态', sortable: true, width: '150', formatter:(row,column) => this.accountStateFormater(this.getaccountStates(),row,column) },
      { prop: 'role', label: '角色', sortable: true, width: '150', formatter: (row, column) => this.roleFormater(store.state.roles, row, column)  },
      { prop: 'lastLogin', label: '上次登录时间', sortable: true, width: '160', formatter: this.timeStampFormater }
    ]
  }

  stringFormater(row, column) {
    if (isNotEmpty(row, column)) {
      return row[column.property]
    }
    return ''
  }

  roleFormater(roles,row, column){
    var role = row[column.property]
    for(var index in roles){
      if(roles[index].id == role){
        return roles[index].name
      }
    }
    return ''
  }

   getCategoryName(categories, val, propName, retrunPropName) {
    for (var index in categories) {
      if (categories[index][propName] == val) {
        return categories[index][retrunPropName]
      }
    }
    return ''
  }

  static timeStamp2String(time) {
    var datetime = new Date();
    datetime.setTime(time * 1000);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1;
    var date = datetime.getDate();
    var hour = datetime.getHours();
    var minute = datetime.getMinutes();
    // var second = datetime.getSeconds();
    // var mseconds = datetime.getMilliseconds();
    return year + "/" + month + "/" + date + " " + hour+"点";
  }

  addAccount(data,sucCallback,failedCallback){
    vue.http.post('/hr/user/assign?user=' + data.user+'&personId='+data.uid+'&roleId='+data.role).then(response => {
      console.log("query response" + response);
      if (response.body.result == 1) {
        console.log('query user data id ' + JSON.stringify(response.body));
        sucCallback(response)
      }
      else{
        failedCallback(response)
      }
    }, response => {
      failedCallback(response)
    })
  }

  deleteAccount(data,sucCallback,failedCallback){
    vue.http.delete('/hr/user/delete?user=' + data.user).then(response => {
      console.log("delete account response" + response);
      if (response.body.result == 1) {
        console.log('delete user data id ' + JSON.stringify(response.body));
        sucCallback(response)
      }
      else{
        failedCallback(response)
      }
    }, response => {
      failedCallback(response)
    })
  }

  updateAccount(data,sucCallback,failedCallback){
    vue.http.post('/hr/user/update?user=' + data.user+'&role='+data.role).then(response => {
      console.log("update account response" + response);
      if (response.body.result == 1) {
        console.log('update user account id ' + JSON.stringify(response.body));
        sucCallback(response)
      }
      else{
        failedCallback(response)
      }
    }, response => {
      failedCallback(response)
    })
  }

  getDefaultUserQueryData(type){
    return {
      type:type,
      conditions: [],
      // orders: {
      //   fields: 'name',
      //   desc: 0
      // },
      page: {
        count: 9999,
        number: 0
      }
    }
  }

  timeStampFormater(row, column) {
    if (isNotEmpty(row, column)) {
      return accountUtils.timeStamp2String(row[column.property])
    }
    return ''
  }

  accountStateFormater(accountStates,row, column) {
    if (isNotEmpty(row, column)) {
      for (var index in accountStates) {
        if (accountStates[index].id == row[column.property]) {
          return accountStates[index].name
        }
      }
    }
    return ''
  }

  getAccountData(id, sucCallback, failedCallback) {
    vue.http.post('/hr/user/list?departmentId=' + id).then(response => {
      console.log("query response" + response);
      if (response.body.result == 1) {
        console.log('query user data id ' + JSON.stringify(response.body));
        sucCallback(response)
      }
    }, response => {
      failedCallback(response)
    })
  }


}